package com.example.wiki.auth.controller;


import com.alibaba.fastjson.JSONObject;
import com.example.wiki.req.UserLoginReq;
import com.example.wiki.req.UserQueryReq;
import com.example.wiki.req.UserResetPasswordReq;
import com.example.wiki.req.UserSaveReq;
import com.example.wiki.resp.CommonResp;
import com.example.wiki.resp.UserLoginResp;
import com.example.wiki.resp.UserQueryResp;
import com.example.wiki.resp.PageResp;

import com.example.wiki.auth.service.GiteeOAuthService;
import com.example.wiki.auth.service.UserService;
import com.example.wiki.utils.SnowFlake;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.concurrent.TimeUnit;


@Api(tags = "用户的增删改查")
@RestController
@RequestMapping("/user")
public class UserController {


    private static final Logger Log = LoggerFactory.getLogger(UserController.class);

    @Resource
    private UserService userService;
    @Resource
    private SnowFlake snowFlake;
    @Resource
    private RedisTemplate redisTemplate;


    @ApiOperation("获取用户及其分类  ")
    @GetMapping("/list")
    public CommonResp list(@Valid UserQueryReq req) {
        CommonResp<PageResp<UserQueryResp>> resp = new CommonResp<>();


        PageResp<UserQueryResp> list = userService.list(req);
        if (list.getTotal() == 0) {
            resp.setSuccess(false);
            resp.setStatus(201);
            resp.setMessage("请求用户列表失败，没有用户");
            return resp;
        }
        resp.setMessage("请求用户列表成功");
        resp.setContent(list);
        return resp;
    }

    @ApiOperation("保存用户")
    @PostMapping("/save")
    public CommonResp save(@Valid @RequestBody UserSaveReq req) {
        CommonResp resp = new CommonResp<>();
        userService.save(req);
        return resp;
    }

    @ApiOperation("删除用户")
    @DeleteMapping("/delete/{id}")
    public CommonResp delete(@PathVariable Long id) {
        CommonResp resp = new CommonResp<>();
        userService.delete(id);
        return resp;
    }

    @ApiOperation("重置密码")
    @PostMapping("/reset-password")
    public CommonResp resetPassword(@Valid @RequestBody UserResetPasswordReq req) {
        req.setPassword(req.getPassword());
        CommonResp resp = new CommonResp<>();
        userService.resetPassword(req);
        return resp;
    }

    @ApiOperation("登录")
    @PostMapping("/login")
    public CommonResp login(@Valid @RequestBody UserLoginReq req) {
        req.setPassword(req.getPassword());
        CommonResp<UserLoginResp> resp = new CommonResp<>();
        UserLoginResp userLoginResp = userService.login(req);

        long token = snowFlake.nextId();
        Log.info("生成单点登录token：{}，并放入redis中", token);
        userLoginResp.setToken(String.valueOf(token));
        redisTemplate.opsForValue().set(String.valueOf(token), JSONObject.toJSONString(userLoginResp), 3600 * 24, TimeUnit.SECONDS);
        resp.setContent(userLoginResp);
        return resp;


    }

    @ApiOperation("登出")
    @GetMapping("/logout/{token}")
    public CommonResp logout(@PathVariable String token) {
        CommonResp resp = new CommonResp<>();
        Log.info("从redis中删除token: {}", token);
        redisTemplate.delete(token);
        Log.info("从redis中删除token: {}", token);
        return resp;
    }


}
